/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatGui.java
 *
 * Created on 31 mars 2010, 09:48:50
 */

package views;

import javax.swing.JFileChooser;
import controllers.ChatController;
import java.awt.event.MouseListener;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author tlnguyen
 */
public class ChatGui extends javax.swing.JFrame implements Runnable {
    
    private  ChatController cont;

    /** Creates new form ChatGui */
    public ChatGui() {
        initComponents();
        initListeners();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginField = new javax.swing.JTextField();
        loginLabel = new javax.swing.JLabel();
        connectButton = new javax.swing.JButton();
        disconnectButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        toSendMsgArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        contactList = new javax.swing.JList();
        sendMsgButton = new javax.swing.JButton();
        fileNameField = new javax.swing.JTextField();
        fileBrowserButton = new javax.swing.JButton();
        sendFileButton = new javax.swing.JButton();
        sendFileLabel = new javax.swing.JLabel();
        rcvdMsgLabel = new javax.swing.JLabel();
        toSendMsgLabel = new javax.swing.JLabel();
        contaclistLabel = new javax.swing.JLabel();
        notificationsLabel = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        rcvdMsgArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        loginLabel.setText("Login:");

        connectButton.setText("Connect");

        disconnectButton.setText("Disconect");
        disconnectButton.setEnabled(false);
        if (disconnectButton.isEnabled()){
        }

        toSendMsgArea.setColumns(20);
        toSendMsgArea.setRows(5);
        toSendMsgArea.setText("\n");
        jScrollPane1.setViewportView(toSendMsgArea);

        contactList.setEnabled(false);
        jScrollPane2.setViewportView(contactList);

        sendMsgButton.setText("Send Message");
        sendMsgButton.setEnabled(false);

        fileNameField.setEditable(false);
        fileNameField.setText("Please chose a file to send");
        fileNameField.setEnabled(false);

        fileBrowserButton.setText("Browse");
        fileBrowserButton.setEnabled(false);
        if (fileBrowserButton.isEnabled()){
        }

        sendFileButton.setText("Send File");
        sendFileButton.setEnabled(false);

        sendFileLabel.setText("Chose a file to send");

        rcvdMsgLabel.setText("Received Messages");

        toSendMsgLabel.setText("Type a message to send");

        contaclistLabel.setText("Contacts");

        notificationsLabel.setForeground(new java.awt.Color(0, 0, 204));
        notificationsLabel.setText("Please login.");

        rcvdMsgArea.setColumns(20);
        rcvdMsgArea.setEditable(false);
        rcvdMsgArea.setRows(5);
        rcvdMsgArea.setEnabled(false);
        jScrollPane3.setViewportView(rcvdMsgArea);

        jLabel1.setText("ChatSystem by TL.NGUYEN, S.NGOYEM, F.HerLastName. Copyright. All rights reversed.");
        jLabel1.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(loginLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(loginField, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(connectButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(disconnectButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(notificationsLabel))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                            .addComponent(toSendMsgLabel)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(contaclistLabel)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(sendMsgButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(rcvdMsgLabel)
                    .addComponent(sendFileLabel)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(fileNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fileBrowserButton)
                        .addGap(12, 12, 12)
                        .addComponent(sendFileButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(connectButton)
                    .addComponent(disconnectButton)
                    .addComponent(notificationsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rcvdMsgLabel)
                    .addComponent(contaclistLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toSendMsgLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(sendMsgButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sendFileLabel)
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fileNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sendFileButton)
                    .addComponent(fileBrowserButton))
                .addGap(18, 18, 18)
                .addComponent(jLabel1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Init all the buttons's mouse listeners
     */
    private void initListeners(){
        connectButtonListener = new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                connectButtonMouseClicked(evt);
            }
        };
        disconnectButtonListener = new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                disconnectButtonMouseClicked(evt);
            }
        };
        sendMsgButtonListener = new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sendMsgButtonMouseClicked(evt);
            }
        };
        sendFileButtonListener = new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sendFileButtonMouseClicked(evt);
            }
        };
        fileBrowserButtonListener = new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileBrowserButtonMouseClicked(evt);
            }
        };
        contactListListener = new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                contactListValueChanged(evt);
            }
        };
        disconnectedGui(); // Activate the Connect Button
    }

    /**
     * Deactivate the Connect Button and activate all the others
     */
    private void connectedGui(){
        loginField.setEnabled(false);
        toSendMsgArea.setEnabled(true);
        connectButton.removeMouseListener(connectButtonListener);
        connectButton.setEnabled(false);
        disconnectButton.addMouseListener(disconnectButtonListener);
        disconnectButton.setEnabled(true);
        sendMsgButton.addMouseListener(sendMsgButtonListener);
        sendMsgButton.setEnabled(true);
        sendFileButton.addMouseListener(sendFileButtonListener);
        sendFileButton.setEnabled(true);
        fileBrowserButton.addMouseListener(fileBrowserButtonListener);
        fileBrowserButton.setEnabled(true);
        contactList.addListSelectionListener(contactListListener);
        contactList.setEnabled(true);
    }

    /**
     * Activate the Connect Button and deactivate all the others
     */
    private void disconnectedGui(){
        loginField.setEnabled(true);
        toSendMsgArea.setEnabled(false);
        connectButton.addMouseListener(connectButtonListener);
        connectButton.setEnabled(true);
        disconnectButton.removeMouseListener(disconnectButtonListener);
        disconnectButton.setEnabled(false);
        sendMsgButton.removeMouseListener(sendMsgButtonListener);
        sendMsgButton.setEnabled(false);
        sendFileButton.removeMouseListener(sendFileButtonListener);
        sendFileButton.setEnabled(false);
        fileBrowserButton.removeMouseListener(fileBrowserButtonListener);
        fileBrowserButton.setEnabled(false);
        contactList.removeListSelectionListener(contactListListener);
        contactList.setEnabled(false);
    }

    /**
     * Login
     * Handle the MouseCliked event on the ConnectButton
     * @param evt
     */
    private void connectButtonMouseClicked(java.awt.event.MouseEvent evt) {
        if (loginField.getText().equals("") || loginField.getText() == null) {
            notificationsLabel.setText("Please give you nickname before pressing the Connect button.");
        } else {
            // deactivate the ConnectButton and activate all the others
            connectedGui();
            // invoke the controller
            cont.performConnect(loginField.getText());
            // nitify the user
            notificationsLabel.setText("Logged in.");
        }
    }

    /**
     * Logout
     * Handle the MouseCliked event on the DisconnectButton
     * @param evt
     */
    private void disconnectButtonMouseClicked(java.awt.event.MouseEvent evt) {
        // active the ConnectButton and deactivate all the others
        disconnectedGui();
        // invoke the controller
        cont.performDisconnect();
        // notify the user
        notificationsLabel.setText("Logged out. See you soon.");
    }

    private void contactListValueChanged(javax.swing.event.ListSelectionEvent evt) {
        // TODO add your handling code here:
    }

    private void sendMsgButtonMouseClicked(java.awt.event.MouseEvent evt) {
    }

    private void fileBrowserButtonMouseClicked(java.awt.event.MouseEvent evt) {

        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(loginField);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            java.io.File file = fc.getSelectedFile();
            fileNameField.setText(file.getAbsolutePath());
            notificationsLabel.setText(file.getName() + " is choosen. Click now on the Send File button.");
        } else {
            notificationsLabel.setText("No file choosen.");
        }

    }

    private void sendFileButtonMouseClicked(java.awt.event.MouseEvent evt) {

    }


    public void refreshMsg (String m) {
    }

    public void refreshFile (String m) {
    }

    /**
     * Add a contact given the nickname to the list
     * @param nickname
     */
    public void addContact (String nickname) {
      refreshContactList();
      notificationsLabel.setText(nickname + " is now connected.");
    }

    /**
     * Remove a contact given the nickname from the list
     * @param nickname
     */
    public void removeContact (String nickname) {
        refreshContactList();
        notificationsLabel.setText(nickname + " is now disconnected.");
    }

    /**
     * Refresh the contact list after adding or removing contacts
     */
    public void refreshContactList() {
      contactList.setModel(new javax.swing.AbstractListModel() {
          String[] strings = cont.getLContacts().toStringTable();
          public int getSize() { return strings.length; }
          public Object getElementAt(int i) { return strings[i]; }
      });
    }

    /**
     * Set the controller of the gui
     * @param cont
     */
    public void setController(ChatController cont){
        this.cont = cont;
    }

    public void run() {
        setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel contaclistLabel;
    private javax.swing.JList contactList;
    private javax.swing.JButton disconnectButton;
    private javax.swing.JButton fileBrowserButton;
    private javax.swing.JTextField fileNameField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField loginField;
    private javax.swing.JLabel loginLabel;
    private javax.swing.JLabel notificationsLabel;
    private javax.swing.JTextArea rcvdMsgArea;
    private javax.swing.JLabel rcvdMsgLabel;
    private javax.swing.JButton sendFileButton;
    private javax.swing.JLabel sendFileLabel;
    private javax.swing.JButton sendMsgButton;
    private javax.swing.JTextArea toSendMsgArea;
    private javax.swing.JLabel toSendMsgLabel;
    // End of variables declaration//GEN-END:variables

    // All the button's mouse listeners
    private MouseListener connectButtonListener;
    private MouseListener disconnectButtonListener;
    private MouseListener fileBrowserButtonListener;
    private MouseListener sendMsgButtonListener;
    private MouseListener sendFileButtonListener;
    private ListSelectionListener contactListListener;
}
